package com.hzdesk.media.koreabatchmatvector.contral;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.base.MyClientApplication;
import com.hzdesk.media.koreabatchmatvector.bean.PathBean;
import com.hzdesk.media.koreabatchmatvector.bean.SerPath;
import com.hzdesk.media.koreabatchmatvector.bean.SerPoint;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.inter.ISketchpadDraw;

import java.util.Date;

import static android.graphics.Path.Direction.CCW;

/**
 * 
 * @author Mr.Lee
 *
 */
public class OvaluCtl implements ISketchpadDraw {
	private Paint mPaint=new Paint();
    private boolean m_hasDrawn = false; 
    private RectF rectf=new RectF();
    private Path path=new Path();
    private Path mPath;
    private boolean status=false;
    private String color;
    private int size;
    private SerPath mCurPath;
    private long time;
    private int num;
    private Context mContext;
	private float startx = 0;
	private float starty = 0;
	private float endx = 0;
	private float endy = 0;
    public OvaluCtl(int penSize, String penColor,Context c)
    {
		mContext=c;
    	mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.parseColor("#"+penColor));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(penSize);
        this.color=penColor;
        this.size=penSize;
    }
    public OvaluCtl(Paint paint, Path path) {
		mPaint=paint;
		mPath=path;
		status=true;
	}
    public OvaluCtl(Paint paint) {
    	mPaint=paint;
	}
	
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		if (status) {
			canvas.drawPath(mPath, mPaint);
			mPath=null;
			status=false;
		}
		else {
			
			if (null != canvas)
			{
				//Log.e(OvaluCtl.class.getSimpleName(),"tkesjt+++++++++++"+rectf.right+"--"+rectf.bottom);
				//Log.e(OvaluCtl.class.getSimpleName(),"tkesjt+++++++++++");
				float x=endx-startx;
				float y=endy-starty;
				if (x>=0&&y>=0) {
					canvas.drawOval(startx, starty, endx, endy, mPaint);
				} else if (x>=0&& y<=0) {
					canvas.drawOval(startx, endy, endx, starty, mPaint);
				} else if (x<0 && y<0) {
					canvas.drawOval(endx, endy, startx, starty, mPaint);
				} else if (x<0 && y>0) {
					canvas.drawOval(endx, starty, startx, endy, mPaint);
				}else {
				}


			}
		}
		
	}
	public boolean hasDraw() {
		// TODO Auto-generated method stub
		return m_hasDrawn;
		//return false;
	}	
	public void cleanAll() {
		// TODO Auto-generated method stub	
	}	
	public void touchDown(float x, float y) {
		// TODO Auto-generated method stub
		rectf.left=x;
		rectf.top=y;
		rectf.right=x;
		rectf.bottom=y;

		startx=x;
		starty=y;
		endx=x;
		endy=y;
		//Log.d("lk down",rectf.toString());
		num++;
		if (MyClientApplication.isRecording()) {
			time=getTime();
			mCurPath=new SerPath();
			mCurPath.mPoints.add(new SerPoint(x, y));
			mCurPath.mTimes.add(getTime());
		}
	}
	public void touchMove(float x, float y) {
		// TODO Auto-generated method stub
		rectf.right=x;
		rectf.bottom=y;
		endx = x;
		endy = y;
		m_hasDrawn=true;
		//Log.d("lk move",rectf.toString());
		num++;
		if ( MyClientApplication.isRecording()) {
			mCurPath.mPoints.add(new SerPoint(x, y));
			mCurPath.mTimes.add(getTime());
		}
	}	
	public void touchUp(float x, float y) {
		// TODO Auto-generated method stub
		rectf.right=x;
		rectf.bottom=y;
		endx = x;
		endy = y;
		//Log.d("lk up",rectf.toString());
		num++;
	//	Log.d("Oval", "size:"+num);
		if (MyClientApplication.isRecording() && MyClientApplication.getMouseUserDeviceinfoBean(mContext,MyClientApplication.getUserNum()).getDisMouse()!=1) {
			mCurPath.mTimes.add(getTime());
			mCurPath.mPoints.add(new SerPoint(x, y));
			//Log.d("Oval", "size:"+mCurPath.mPoints.size());
			MyClientApplication.setmPathBeanList(new PathBean(time, AppConfig.PAINT_OVAL, color, size, mCurPath, MyClientApplication.getUserNum()));
		}
	}
	public long getTime() {
		return new Date().getTime() - MyClientApplication.getStartTime();
	}

}
